/*
============================================================================
FileName	:	MainDialog.cpp
Author		:	liyake
Website		:	http://www.liyake.com
Create Data	:	2012-03-08 13:07
Change Data	:	2012-03-08 13:07
Detail		:	The main dialog of xqchat application
============================================================================
*/

#include "MainDialog.h"
#include "XQChatClientManager.h"

using namespace XQChat;

CMainDialog* XQChat::CMainDialog::m_pMainDialog = 0;
CMainDialog* XQChat::CMainDialog::getMainDialog()
{
	if(!m_pMainDialog)
		m_pMainDialog = new CMainDialog();

	return m_pMainDialog;
}

XQChat::CMainDialog::CMainDialog( QWidget* parent /*= 0 */ )
	: QMainWindow(parent)
	, m_pCentralWidget(0)
{
	m_pCentralWidget = new QWidget(this);

#ifdef XQCHAT_LIST_MODE
	m_pRosterView = new CXQChatRosterListView(this);
	m_pRosterModel = new CXQChatRosterItemModel(this);
	m_pRosterView->setModel(m_pRosterModel);
	rosterItemDelegate *delegate = new rosterItemDelegate();
	m_pRosterView->setItemDelegate(delegate);
#else
	m_pRosterView = new CXQChatRosterView(this);
#endif


	QGridLayout* pLayoutMain = new QGridLayout(m_pCentralWidget);
	pLayoutMain->addWidget(m_pRosterView,0,0,1,1);

	m_pCentralWidget->setLayout(pLayoutMain);

	setCentralWidget(m_pCentralWidget);

	connect(CXQChatClientManager::getQXChatClientManager(),SIGNAL(rosterReceived()),
		this,SLOT(onRosterReceived()));
	connect(CXQChatClientManager::getQXChatClientManager(),SIGNAL(presenceChanged(const QString&, const QString&)),
		this,SLOT(onPresenceChanged(const QString&, const QString&)));
}

XQChat::CMainDialog::~CMainDialog(void)
{

}

void XQChat::CMainDialog::onRosterReceived()
{
	CXQChatRosterManager* pRosterManager = CXQChatClientManager::getQXChatClientManager()->getRosterManager();
	QList<CXQChatRoster*> listRoster = pRosterManager->getRostersAll();

    foreach(CXQChatRoster* pRoster, listRoster)
	{
#ifdef XQCHAT_LIST_MODE
		m_pRosterModel->updateRoster(pRoster);
#else
		m_pRosterView->updateRoster(pRoster);
#endif
	}
}

void XQChat::CMainDialog::onPresenceChanged( const QString& fullJid, const QString& resource )
{

}

